package com.workcell.workdata.xthings.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.workcell.workdata.common.core.util.R;
import com.workcell.workdata.common.excel.annotation.ResponseExcel;
import com.workcell.workdata.xthings.dto.BaseVo;
import com.workcell.workdata.xthings.entity.BaseSerialDevice;
import com.workcell.workdata.xthings.entity.Manufacturer;
import com.workcell.workdata.xthings.service.BaseSerialDeviceService;
import com.workcell.workdata.xthings.service.ManufacturerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 串口设备接口
 *
 * @author Huang Zengtian
 */
@Api(tags = "串口设备")
@RestController
@RequestMapping("/baseSerialDevice")
@RequiredArgsConstructor
public class BaseSerialDeviceController {
    private final BaseSerialDeviceService baseSerialDeviceService;
    private final ManufacturerService manufacturerService;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public R page(Page page, BaseSerialDevice baseSerialDevice) {
        return R.ok(baseSerialDeviceService.page(page, Wrappers.query(baseSerialDevice)));
    }

    @ApiOperation(value = "模拟分页查询", notes = "模拟分页查询")
    @GetMapping("/vaguePage")
    public R getBasePowermeterVaguePage(BaseVo baseVo) {
        return R.ok(baseSerialDeviceService.getBaseSerialDeviceVaguePage(baseVo));
    }

    @ApiOperation("通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") String id) {
        BaseSerialDevice baseSerialDevice = baseSerialDeviceService.getById(id);
        return R.ok(baseSerialDevice);
    }

    @ApiOperation("新增串口设备型号")
    @PostMapping("/save")
    public R save(@RequestBody BaseSerialDevice baseSerialDevice) {
        Manufacturer manufacturer = manufacturerService.selectByManufacturerCode(baseSerialDevice.getDeviceManufacturer());
        Assert.notNull(manufacturer, "厂商查询为空");
        baseSerialDevice.setManufacturerShortName(manufacturer.getManufacturerShortName());
        baseSerialDevice.setManufacturerFullName(manufacturer.getManufacturerFullName());
        baseSerialDevice.setManufacturer(manufacturer.getManufacturerFullName());
		baseSerialDevice.setManufacturerCode(manufacturer.getManufacturerCode());
        boolean save = baseSerialDeviceService.save(baseSerialDevice);
        return R.ok(save);
    }

    @ApiOperation("修改串口设备型号（不能修改状态）")
    @PostMapping("/updateBaseSerialDeviceById")
    public R update(@RequestBody BaseSerialDevice baseSerialDevice) {
		Manufacturer manufacturer = manufacturerService.selectByManufacturerCode(baseSerialDevice.getDeviceManufacturer());
		Assert.notNull(manufacturer, "厂商查询为空");
		baseSerialDevice.setManufacturerShortName(manufacturer.getManufacturerShortName());
		baseSerialDevice.setManufacturerFullName(manufacturer.getManufacturerFullName());
		baseSerialDevice.setManufacturer(manufacturer.getManufacturerFullName());
		baseSerialDevice.setManufacturerCode(manufacturer.getManufacturerCode());
        boolean updateById = baseSerialDeviceService.updateBaseSerialDeviceById(baseSerialDevice);
        return R.ok(updateById);
    }

    @ApiOperation("修改串口设备型号状态")
    @PostMapping("/updateStateById")
    public R updateStateById(@RequestBody BaseSerialDevice baseSerialDevice) {
        boolean updateStateById = baseSerialDeviceService.updateStateById(baseSerialDevice);
        return R.ok(updateStateById);
    }

    @ApiOperation("删除串口设备型号")
    @DeleteMapping("/{id}")
    public R removeBaseSerialDeviceById(@PathVariable("id") String id) {
        boolean removeById = baseSerialDeviceService.removeBaseSerialDeviceById(id);
        return R.ok(removeById);
    }

    @ApiOperation("导出Excel")
    @ResponseExcel
    @GetMapping("/export")
    public List<BaseSerialDevice> export(BaseSerialDevice baseSerialDevice) {
        return baseSerialDeviceService.list(Wrappers.query(baseSerialDevice));
    }

}
